package com.cljh.controller;

import com.cljh.bean.Dict;
import com.cljh.bean.Path;
import com.cljh.bean.Role;
import com.cljh.controller.page.Code;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.Result;
import com.cljh.mapper.DictMapper;
import com.cljh.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/menus")
public class MenuController {
    @Autowired
    MenuService menuService;

    @Autowired
    DictMapper dictMapper;
    @PostMapping
        public Result save(@RequestBody Path role) {
        System.out.println(role);
        boolean save = menuService.save(role);
        Integer code = save ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg = save ? "添加成功" : "添加失败";
        return new Result(code, save, msg);
    }


    @DeleteMapping("/{id}")
    public Result delete( @PathVariable Integer id) {
        boolean delete = menuService.delete(id);
        Integer code=delete ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=delete ? "删除一个成功":"删除一个失败";
        return new Result(code,delete,msg);
    }

    @PutMapping
    public Result Upd(@RequestBody Path role) {
        boolean update = menuService.Upd(role);
        Integer code=update ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg=update ? "修改成功":"修改失败";
        return new Result(code,update,msg);
    }

    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {
        Path role = menuService.selectById(id);
        Integer code=role !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=role!=null ? "查询到信息":"未查询到信息";
        return new Result(code,role,msg);
    }

    @GetMapping("/get")
    public Result selectAll(@RequestParam Integer currentPage, @RequestParam Integer pageSize, @RequestParam(defaultValue = "") String menuName) {

//        System.out.println(currentPage);
//        System.out.println(pageSize);
//        System.out.println(name);
//        System.out.println(description);
        PageBean<Path> rolePageBean = menuService.selectAll(currentPage, pageSize, menuName);

        Integer code=rolePageBean !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=rolePageBean!=null ? "查询到信息":"未查询到信息";
        return new Result(code,rolePageBean,msg);
    }

    @DeleteMapping("/deleteIds")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        boolean b = menuService.deleteIds(ids);
        Integer code=b ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=b ? "删除成功":"删除失败";
        return new Result(code,b,msg);
    }


    @GetMapping("/all")
    public Result selectByAllId() {
        List<Path> role = menuService.selectAllPath();

        Integer code=role !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=role!=null ? "查询到信息":"未查询到信息";
        return new Result(code,role,msg);
    }

    @GetMapping("/dict")
    public Result getDict() {
        List<Dict> role= dictMapper.selectByType(Code.DICT_TYPE);
        Integer code=role !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=role!=null ? "查询到信息":"未查询到信息";
        return new Result(code,role,msg);
    }


}
